function fun(selector){
	var result = document.querySelectorAll(selector);
	if(result.length === 1){
		return result[0]
	}
	return result;
}

class Cart{
	constructor() {
	    this.eventBind();
	}
	
	//获取且更新总货物总数量
	getGoodsNumAndUpdate(){
		//获取所有商品的数量
		let oGoodsNum = fun(".number");
		let onumber = fun("#number")
		//存放商品数量叠加的总值
		oGoodsNum.innerHTML = onumber.value;
	}
	
	
	// 获取且更新货物总价格
	getGoodsPriceAndUpdate(){
		let oGoodsPrice = fun("#singleprice");
		let onumber = fun("#number");
		let oGoodsPrice1 = String(oGoodsPrice.innerHTML);
		let oGoodsPrice2 = oGoodsPrice1.slice(1,3);
		let goodsTotalPrice = Number(oGoodsPrice2)* Number(onumber.value);
		let oGoodsAllPrice = fun(".allprice");
		oGoodsAllPrice[0].innerHTML = String("￥" + goodsTotalPrice);
		oGoodsAllPrice[1].innerHTML = String("￥" + goodsTotalPrice);
		oGoodsAllPrice[2].innerHTML = String("￥" + goodsTotalPrice);
	}

	//增加获取
	//参数btn就是你点击的按钮
	addGoods(btn){
		//获取数量
		let oGoodsNum = btn.previousElementSibling;
		//1.
		oGoodsNum.value = Number(oGoodsNum.value) + 1; 
		//3.
		this.getGoodsNumAndUpdate();
		//4.
		this.getGoodsPriceAndUpdate();
	}	
	
	minGoods(btn){
		//获取数量
		let oGoodsNum = btn.nextElementSibling;
		
		if(oGoodsNum.value > 1){
			oGoodsNum.value = oGoodsNum.value - 1;
			//获取单价
			
			this.getGoodsNumAndUpdate();
			//4.
			this.getGoodsPriceAndUpdate();
		}
	}
	
	delGoods(btn){
		btn.parentNode.parentNode.parentNode.className = "list";
	}
	
	eventBind(){
			let oBtns1 = fun(".plus");
			//这个this是为了避免在事件体内cart的对象被覆盖
			let that = this;
			oBtns1.onclick = function(){
						that.addGoods(this);
						
					}
			let oBtns2 = fun(".reduce")	
			oBtns2.onclick = function(){
						that.minGoods(this);
					}
			
			
			let odel = fun(".del");
			odel.onclick = function(){
					that.delGoods(this);
				}
	}
	
	
}
let c = new Cart();





// 轮播图
let Next = fun(".fav .button-next");
let Prev = fun(".fav .button-prev");
let ul = fun(".fav ul");
let liList=fun(".fav ul li")
let n = 0;



function move(dir = "right", count = 1, speed = 20) {
	if (dir === "right") {
		let move = setInterval(() => {
			let ml = parseInt(ul.style.marginLeft);
			if (ml <= -(n * 889)) {
				clearInterval(move)
				ul.style.marginLeft = -(n * 889) + "px";
				return;
			}
			ul.style.marginLeft = ml - speed * count + "px";
		}, 16)
	}
	if (dir === "left") {
		let move = setInterval(() => {
			let ml = parseInt(ul.style.marginLeft);
			if (ml >= -(n * 889)) {
				clearInterval(move)
				ul.style.marginLeft = -(n * 889) + "px";
				return;
			}
			ul.style.marginLeft = ml + speed * count + "px";
		}, 16)
	}
	
}

function move0(speed = 20) {
	let virtualNode = liList[0].cloneNode(true);
	virtualNode.className = "virtualNode"
	ul.appendChild(virtualNode)

	let move = setInterval(() => {
		let ml = parseInt(ul.style.marginLeft);
		if (ml <= -((liList.length-7) * 889)) {
			clearInterval(move)
			ul.style.marginLeft = -((liList.length-7) * 889) + "px";
			virtualNode.remove();
			ul.style.marginLeft = "0px";
			return;
		}
		ul.style.marginLeft = ml - speed + "px";
	}, 16)

	
}

function move1(speed = 20) {
	let virtualNode = liList[(liList.length-7) - 1].cloneNode(true);
	virtualNode.className = "virtualNode"
	ul.insertBefore(virtualNode, liList[0])
	ul.style.marginLeft = "-889px";

	let move = setInterval(() => {
		let ml = parseInt(ul.style.marginLeft);
		if (ml >= 0) {
			clearInterval(move)
			ul.style.marginLeft = 0 + "px";
			virtualNode.remove();
			ul.style.marginLeft = -((liList.length-7)- 1) * 889 + "px"
			return;
		}
		ul.style.marginLeft = ml + speed + "px";
	}, 16)

	
}


Next.onclick = function() {
	if (n !== (liList.length-7) - 1) {
		n++;
		move();
		
		
	} else {
		n = 0;
		move0();
	}
}

Prev.onclick = function() {
	if (n !== 0) {
		n--;
		move("left");
	} else {
		n = (liList.length-7)- 1
		move1();
	}
}
